using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class PostDal : Base
    {
        public PostDal() : base() { }

        public enum Fields
        {
            Id,
            SiteId,
            SiteName,
            ParentId,
            ParentPath,
            Name,
            DisplayName,
            TemplateId,
            TemplateName,
            ThumbnailId,
            Summary,
            Tags,
            Sequence,
            CreatedDateTime
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.SiteName, reader, table);
            GetReaderValue<int?>(Fields.ParentId, reader, table);
            GetReaderValue<string>(Fields.ParentPath, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<string>(Fields.DisplayName, reader, table);
            GetReaderValue<int>(Fields.TemplateId, reader, table);
            GetReaderValue<string>(Fields.TemplateName, reader, table);
            GetReaderValue<int?>(Fields.ThumbnailId, reader, table);
            GetReaderValue<string>(Fields.Summary, reader, table);
            GetReaderValue<string>(Fields.Tags, reader, table);
            GetReaderValue<int>(Fields.Sequence, reader, table);
            GetReaderValue<DateTime>(Fields.CreatedDateTime, reader, table);
        }

        public Hashtable GetTopLevelBySiteId(int siteId)
        {
            SqlCommand cmd = CreateCommand("GetTopLevelBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetTopLevelBySiteIdAndMenuId(int siteId, int menuId)
        {
            SqlCommand cmd = CreateCommand("GetTopLevelByMenuIdAndSiteId");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllBySiteIdMenuIdAndParentPath(int siteId, int menuId, string parentPath)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteIdMenuIdAndParentPath");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentPath", parentPath, SqlDbType.VarChar, 255);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllBySiteIdParentPath(int siteId, string parentPath)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteIdMenuIdAndParentPath");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentPath", parentPath, SqlDbType.VarChar, 255);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByMenuIdAndParentId(int menuId, int parentId)
        {
            SqlCommand cmd = CreateCommand("GetAllByMenuIdAndParentId");
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByParentId(int parentId)
        {
            SqlCommand cmd = CreateCommand("GetAllByParentId");
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetBySiteUrlAndPostUrl(string siteUrl, string postUrl)
        {
            SqlCommand cmd = CreateCommand("GetBySiteUrlAndPostUrl");
            AddInputParameter(cmd, "SiteUrl", siteUrl, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "PostUrl", postUrl, SqlDbType.VarChar, 255);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetBySiteIdAndUrl(int siteId, string url)
        {
            SqlCommand cmd = CreateCommand("GetBySiteIdAndUrl");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Url", url, SqlDbType.VarChar, 255);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetBySiteIdMenuIdAndUrl(int siteId, int menuId, string url)
        {
            SqlCommand cmd = CreateCommand("GetBySiteIdMenuIdAndUrl");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "Url", url, SqlDbType.VarChar, 255);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public void Update(ref int id, int siteId, ref string siteName, int? parentId, ref string parentPath, string name, string displayName, int templateId, ref string templateName, int? thumbnailId, string summary, string tags, ref int sequence, ref DateTime createdDateTime, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "DisplayName", displayName, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            AddInputParameter(cmd, "ThumbnailId", thumbnailId, SqlDbType.Int);
            AddInputParameter(cmd, "Summary", summary, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Tags", tags, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Sequence", sequence, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "SiteNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "ParentPathOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "TemplateNameOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "SequenceOut", SqlDbType.Int);
            AddOutputParameter(cmd, "CreatedDateTimeOut", SqlDbType.DateTime);
            AddOutputParameter(cmd, "VersionIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            siteName = GetOutputValue<string>(cmd, "SiteNameOut");
            parentPath = GetOutputValue<string>(cmd, "ParentPathOut");
            templateName = GetOutputValue<string>(cmd, "TemplateNameOut");
            sequence = GetOutputValue<int>(cmd, "SequenceOut");
            createdDateTime = GetOutputValue<DateTime>(cmd, "CreatedDateTimeOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "VersionIdOut");

        }

        public void Delete(int id, object versionId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", versionId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }

        public Hashtable[] GetAllByParentIdAndTopicId(int parentId, int topicId, bool includeChildren, int startIndex, int pageSize, ref int totalRecords)
        {
            SqlCommand cmd = CreateCommand("GetAllByParentIdAndTopicId");
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "TopicId", topicId, SqlDbType.Int);
            AddInputParameter(cmd, "IncludeChildren", includeChildren, SqlDbType.Bit);
            return ExecuteGetPagedCollection(cmd, startIndex, pageSize, ref totalRecords);
        }
    }
}
